from django import forms


class BootStrap():
    # 定义一个列表 添加到里面的字段不执行
    bootstrap_exclude_fields = []

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # 循环ModelFrom所有的字段,找到所有在加上属性 "class": "form-control"
        # name是想拿到的名字 field是相对应的对象
        for name, field in self.fields.items():
            if name in self.bootstrap_exclude_fields:
                continue
            # 这里是强行修改所有属性 应该要保留原来字段的属性 没有属性在增减属性
            if field.widget.attrs:
                field.widget.attrs['class'] = 'form-control'
                field.widget.attrs['placeholder'] = field.label
            else:
                field.widget.attrs = {
                    "class": "form-control",
                    "placeholder": field.label}


class BootStrapModelFrom(BootStrap, forms.ModelForm):
    pass


class BootStrapFrom(BootStrap, forms.Form):
    pass
